<?php
/**
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CDb.php 2010-01-15 19:16:38Z andery $
 */
class CDb
{
    // 已存在数据库连接
	protected static $_dbLink = array();
    
    /*架构函数*/
    private function __construct($db_ms) {
        $this->dbfactory($db_ms);
    }
    
    private function dbfactory($db_ms) {
        $db_config = CConf::get('db.'.$db_ms);
	    $db_url = parse_url($db_config['db_url']);
	    if($db_url['scheme']==''){
	        //错误处理：配置文件中类型为空
	        throw new CException(Nimo::lang('DB_CONFIG_ERROR'));
	    }
	    $dbType = ucwords(strtolower($db_url['scheme']));
	    $dbClass = 'C' . $dbType;
	    require_once(dirname(__FILE__) . '/scheme/Ischeme.php');
	    require_once(dirname(__FILE__) . '/scheme/' . $dbClass . '.php');
	    if(class_exists($dbClass)) {
	        self :: $_dbLink[$db_ms] = new $dbClass($db_config);
	    }else {
	        //错误处理： 找不到相应数据库驱动
	    }
    }
    
    public function getInstance($db_ms='default') {
    	if(!array_key_exists($db_ms, self :: $_dbLink)){
    	    new self($db_ms);
    	}
		return self :: $_dbLink[$db_ms];
	}
	
	private function __clone(){}
}